package com.controller.home;

import com.controller.BaseController;
import com.entity.UserAddress;
import com.service.imple.home.UserAddressServiceImple;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收货地址
 */
@Controller
@RequestMapping("home/UserAddress")
public class UserAddressController extends BaseController<UserAddressServiceImple> {

    @RequestMapping("addUserAddress")
    @ResponseBody
    public Map<String, Object> addUserAddress(@SessionAttribute("userId")Integer userId, String accept_name, String province, String city, String area, String address, String email,
                                              String post_code, Integer isDefault, String telphone){

        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setUserName(accept_name);//姓名
        userAddress.setProvince(province);//省
        userAddress.setCity(city);//市
        userAddress.setArea(area);//区
        userAddress.setAddress(address);//详细地址
        userAddress.setEmail(email);//邮箱
        userAddress.setCode(post_code);//邮政编码
        userAddress.setPhone(telphone);//手机号码
        userAddress.setIsDefault(1);
        int count = BaseService.insert(userAddress);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        System.out.println(map);
        return map;
    }

    @RequestMapping("index")
    public String index(@SessionAttribute("userId")Integer userId, ModelMap modelMap){
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> list =  BaseService.SelectAll(userAddress);
        modelMap.put("list",list);
        return "center/address/index";
    }



    @RequestMapping("toEditor")
    public String toEditor(@SessionAttribute("userId")Integer userId, ModelMap modelMap){
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        List<UserAddress> list =  BaseService.SelectAll(userAddress);
        modelMap.put("list",list);
        return "center/address/editor";
    }

    @RequestMapping("Editor")
    @ResponseBody
    public Map<String, Object> Editor(@SessionAttribute("userId")Integer userId, UserAddress userAddress){
        userAddress.setUserId(userId);
        int count = BaseService.update(userAddress);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("Editor1")
    public String Editor1(@SessionAttribute("userId")Integer userId, UserAddress userAddress){
        userAddress.setUserId(userId);
        List<UserAddress> list= BaseService.SelectAll(userAddress);
        list.get(0).setIsDefault(1);
        BaseService.update(list.get(0));
        return "redirect:/home/UserAddress/index";
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "center/address/add";
    }

    @RequestMapping("Add")
    @ResponseBody
    public Map<String,Object> Add(@SessionAttribute("userId")Integer userId, UserAddress userAddress){
        userAddress.setUserId(userId);
        int count = BaseService.insert(userAddress);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }


}
